﻿@using System.Data;
@using UtilsORM.MySqlData;
@using GatewayServer.Common;
@using UtilsBase;

@{
    ViewBag.PageTitle = "指令调试";

    Layout = "~/Views/Shared/PageTemplate.cshtml";
}

<script>
    $(document).ready(function () {
        getDeviceExtInfo("@(ViewBag.d_guid)");
        getDeviceModel("@(ViewBag.d_guid)");
        getValueCurrent("@(ViewBag.d_guid)");
        pagelist_mq("@(ViewBag.d_guid)");
        pagelist_command_log("@(ViewBag.d_guid)");

        setInterval("getDeviceExtInfo('@(ViewBag.d_guid)')", 15000);
        setInterval("getValueCurrent('@(ViewBag.d_guid)')", 5000);
        setInterval("pagelist_mq('@(ViewBag.d_guid)')", 10000);
        setInterval("pagelist_command_log('@(ViewBag.d_guid)')", 10000);
    });


    function getDeviceExtInfo(dguid) {
        $.post("getDeviceExtInfo", { "flag": Math.random(), guid: dguid }, function (result) {
            var myobj = (result.data[0]);
            $("#d_name").text(myobj.d_name);
            $("#d_code").text(myobj.d_code);
            $("#gwname").text(myobj.gwname + "-" + myobj.gwstate);
            $("#d_channel").text(myobj.d_channel);
            $("#d_parameter").val(myobj.d_parameter);
            $("#d_parameter_init").val(myobj.d_parameter_init);
        });
    }
    function getDeviceModel(dguid) {
        $.post("getDeviceModel", { "flag": Math.random(), guid: dguid }, function (result) {
            var myobj = (result.data);
            $("#m_parameter_demo").val(myobj.m_parameter_demo);
            $("#m_parameter_demo_init").val(myobj.m_parameter_demo_init);
            $("#m_command_explain").val(myobj.m_command_explain);
        });
    }

    function getValueCurrent(dguid) {
        $.post("getValueCurrent", { "flag": Math.random(), guid: dguid }, function (result) {
            var pagedata = (result.data);
            rowcount = 0;
            var htmlstr = "";
            htmlstr = htmlstr + "<tr class='plainItemTRTitle'>";
            htmlstr = htmlstr + "<td >名称</td >";
            htmlstr = htmlstr + "<td >变量</td >";
            htmlstr = htmlstr + "<td >变量值</td >";
            htmlstr = htmlstr + "<td >时间</td >";
            htmlstr = htmlstr + "</tr >";
            document.getElementById("pagelist_main").innerHTML = "";
            if (pagedata != null && pagedata.length > 0) {
                for (var i = 0; i < pagedata.length; i++) {
                    rowcount = i + 1;
                    htmlstr = htmlstr + "<tr>";
                    htmlstr = htmlstr + "<td >" + pagedata[i].vc_name + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].vc_key + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].vc_value + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].vc_add_time + "</td >";
                    htmlstr = htmlstr + "</tr >";

                }
                document.getElementById("pagelist_main").innerHTML = htmlstr;
            }
        });
    }
    function pagelist_mq(dguid) {
        $.post("getMQList", { "flag": Math.random(), guid: dguid }, function (result) {
            var pagedata = (result.data);
            rowcount = 0;
            var htmlstr = "";
            htmlstr = htmlstr + "<tr class='plainItemTRTitle'>";
            htmlstr = htmlstr + "<td >类型</td >";
            htmlstr = htmlstr + "<td >内容</td >";
            htmlstr = htmlstr + "<td >时间</td >";
            htmlstr = htmlstr + "<td >结果</td >";
            htmlstr = htmlstr + "</tr >";
            document.getElementById("pagelist_mq").innerHTML = "";
            if (pagedata != null && pagedata.length > 0) {
                for (var i = 0; i < pagedata.length; i++) {
                    rowcount = i + 1;
                    htmlstr = htmlstr + "<tr>";
                    htmlstr = htmlstr + "<td >" + pagedata[i].ml_type + "</td >";
                    htmlstr = htmlstr + "<td  >" + pagedata[i].ml_content + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].ml_add_time + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].ml_result + "</td >";
                    htmlstr = htmlstr + "</tr >";
                }
                document.getElementById("pagelist_mq").innerHTML = htmlstr;
            }
        });
    }
    function pagelist_command_log(dguid) {
        $.post("getCommandLog", { "flag": Math.random(), guid: dguid }, function (result) {
            var pagedata = (result.data);
            rowcount = 0;
            var htmlstr = "";
            htmlstr = htmlstr + "<tr class='plainItemTRTitle'>";
            htmlstr = htmlstr + "<td >名称</td >";
            htmlstr = htmlstr + "<td >key</td >";
            htmlstr = htmlstr + "<td >指令</td >";
            htmlstr = htmlstr + "<td >收到回复</td >";
            htmlstr = htmlstr + "<td >时间</td >";
            htmlstr = htmlstr + "</tr >";
            document.getElementById("pagelist_commandlog").innerHTML = "";
            if (pagedata != null && pagedata.length > 0) {
                for (var i = 0; i < pagedata.length; i++) {
                    rowcount = i + 1;
                    htmlstr = htmlstr + "<tr>";

                    htmlstr = htmlstr + "<td >" + pagedata[i].l_name + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].l_key + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].l_content + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].l_return_msg + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].l_return_time + "</td >";
                    htmlstr = htmlstr + "</tr >";

                }
                document.getElementById("pagelist_commandlog").innerHTML = htmlstr;
            }
        });
    }

    function sendJsonMsg(msg) {
        $("#returnmsg").text("发送中");
        //var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("sendJsonMsg", { msg: msg, dguid: '@(ViewBag.d_guid)' }, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                //alert("发送成功");
                $("#returnmsg").text("发送成功");
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });
    }
    function sendCommand(dcguid, dguid) {
        $("#returnmsg").text("发送中");
        $.post("sendCommand", { dcguid: dcguid, dguid: dguid }, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
               // alert("发送成功");

                $("#returnmsg").text("发送成功");
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });
    }


</script>

<style>
    #pagelist_follow {
        width: 100%;
    }

    #pagelist_main {
        width: 100%;
    }

    .kv-item label {
        width: 150px;
    }

    .plainItemTRTitle td {
        font-weight: bold;
        background-color: #d6d7d7;
        min-width: 80px;
    }

    .mqTable td {
        border: 1px solid #e2e3e4;
        color: #757778;
        line-height: 30px;
    }

    .mqTable {
        width: 100%;
    }
</style>
<div id="saper-container">

    <div id="saper-hd"></div>
    <div id="saper-bd">
        <div class="subfiled clearfix">
            <h2>指令调试</h2>
        </div>
        <div class="subfiled-content">


            <div style="width:40%;float:left;">
                <form id="addForm2" name="addForm2" action="list" method="post">
                    <div style="background-color:#f4f5f6;padding:3px;border-radius:10px;">
                        <div class="kv-item clearfix">
                            <label style="width:200px"><span class="impInfo"></span>协议参数(json格式)：</label>
                            <div class="kv-item-content">

                                <textarea id="d_parameter" name="d_parameter" cols="30" rows="10" style="width:370px;height:100px;"></textarea>

                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label  style="width:200px"><span class="impInfo"></span>设备初始化(json格式)：</label>
                            <div class="kv-item-content">

                                <textarea id="d_parameter_init" name="d_parameter_init" cols="30" rows="10" style="width:370px;height:100px;"></textarea>
                            </div>
                        </div>
                    </div>
                    <div style="background-color:#eaf3fb;padding:3px;margin-top:10px;border-radius:10px;">
                        <div class="kv-item clearfix">
                            <label style="width:200px"><span class="impInfo"></span>设备型号-协议参数(json格式)：</label>
                            <div class="kv-item-content">
                                <textarea id="m_parameter_demo" name="m_parameter_demo" cols="30" rows="10" style="width:370px;height:100px;"></textarea>
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label style="width:200px"><span class="impInfo"></span>设备型号-设备初始化(json格式)：</label>
                            <div class="kv-item-content">
                                <textarea id="m_parameter_demo_init" name="m_parameter_demo_init" cols="30" rows="10" style="width:370px;height:100px;"></textarea>
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label style="width:200px"><span class="impInfo"></span>设备型号-备注：</label>
                            <div class="kv-item-content">
                                <textarea id="m_command_explain" name="m_command_explain" cols="30" rows="10" style="width:370px;height:130px;"></textarea>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div id="" title="添加/编辑" style="width:50%;float:left;margin:10px;">
                <form action="" name="addForm" id="addForm" method="post">

                    <div class="kv-item clearfix">
                        <label><span class="impInfo"></span>设备编号：</label>
                        <div class="kv-item-content">
                            <label id="d_code" style="text-align:left"></label>

                        </div>
                    </div>
                    <div class="kv-item clearfix">
                        <label><span class="impInfo"></span>设备名称：</label>
                        <div class="kv-item-content">
                            <label id="d_name" style="text-align:left"></label>
                        </div>
                    </div>
                    <div class="kv-item clearfix">
                        <label><span class="impInfo"></span>网关：</label>
                        <div class="kv-item-content">
                            <label id="gwname" style="text-align:left"></label>
                        </div>
                    </div>
                    <div class="kv-item clearfix">
                        <label><span class="impInfo"></span>通道：</label>
                        <div class="kv-item-content">
                            <label id="d_channel" style="text-align:left"></label>
                        </div>
                    </div>
                    <div class="kv-item clearfix">
                        <label><span class="impInfo"></span>指令(json格式)：</label>
                        <div class="kv-item-content">
                            <textarea id="jsonMsg" name="jsonMsg" cols="30" rows="10" style="width:420px;height:150px;"></textarea>
                        </div>

                    </div>
                    <div class="kv-item clearfix" style="margin-left:160px">
                        <a href="https://www.sojson.com/" target="_blank">json在线检测工具</a>  &nbsp;&nbsp;
                        <a href="https://jisuan5.com/hexadecimal-to-decimal/" target="_blank">进制转换</a>
                            &nbsp;&nbsp;
                                <a href="http://www.ip33.com/crc.html" target="_blank">modbus校验码</a>
                    </div>
                    <div><hr /></div>
                    <div style="margin:0 auto;width:150px;">
                        <input type="button" id="save" name="save" value="发送指令" onclick="javascript:sendJsonMsg($('#jsonMsg').val())" />
                        &nbsp;&nbsp; <input type="button" id="but_back" name="but_back" value="返回列表" />

                        <br />
                        <label id="returnmsg" name="returnmsg" style="color:red"></label>
                    </div>
                </form>

                <div style="clear:both;height:50px"></div>
                <div>指令集：<br /><hr style="margin-top:1px;" /></div>
                <div style="">
                    <table style="width:100%">
                        <tr class='plainItemTRTitle'>
                            <td>指令名称</td>
                            <td>指令内容</td>
                            <td>操作</td>
                        </tr>
                        @foreach (iot_device_command item in (ViewData["commands"] as List<iot_device_command>))
                        {
                            <tr>
                                <td>@item.dc_name</td>
                                <td>@item.dc_content</td>
                                <td><a href="javascript:void(0)" onclick="sendCommand('@(item.dc_guid)','@(ViewBag.d_guid)')">发送指令</a></td>
                            </tr>
                        }
                    </table>
                </div>
                <div style="margin-top:30px;">设备值：<hr style="margin-top:1px;" /></div>
                <div>
                    <table id="pagelist_main" class="plainItemTable"></table>
                </div>
            </div>


            <div style="clear:both"></div>

            <div style="margin-top:30px;">指令记录：<hr style="margin-top:1px;" /></div>
            <div>
                <table id="pagelist_commandlog" class="mqTable"></table>
            </div>
            <div style="margin-top:30px;">mq消息：<hr style="margin-top:1px;" /></div>
            <div>
                <table id="pagelist_mq" class="mqTable"></table>
            </div>

        </div>
    </div>
    <div id="saper-ft"></div>

</div>

